package com.sakura.mall.generator.util;

import com.sakura.mall.generator.model.NameModel;

/**
 * 字符串工具类
 */
public class StringUtils {
    
    /**
     * 下划线转驼峰命名
     */
    public static String toCamelCase(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        
        str = str.toLowerCase();
        StringBuilder result = new StringBuilder();
        boolean nextUpper = false;
        
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            
            if (c == '_') {
                nextUpper = true;
            } else {
                if (nextUpper) {
                    result.append(Character.toUpperCase(c));
                    nextUpper = false;
                } else {
                    result.append(c);
                }
            }
        }
        
        return result.toString();
    }
    
    /**
     * 下划线转驼峰命名（首字母大写）
     */
    public static String toPascalCase(String str) {
        String camelCase = toCamelCase(str);
        if (camelCase == null || camelCase.isEmpty()) {
            return camelCase;
        }
        
        return Character.toUpperCase(camelCase.charAt(0)) + camelCase.substring(1);
    }
    
    /**
     * 将字符串首字母小写
     */
    public static String lowerFirst(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        
        return Character.toLowerCase(str.charAt(0)) + str.substring(1);
    }
    
    /**
     * 将字符串首字母大写
     */
    public static String upperFirst(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        
        return Character.toUpperCase(str.charAt(0)) + str.substring(1);
    }
    
    /**
     * 去除表名前缀
     */
    public static String removePrefix(String tableName, String prefix) {
        if (tableName == null || tableName.isEmpty() || prefix == null || prefix.isEmpty()) {
            return tableName;
        }
        
        if (tableName.startsWith(prefix)) {
            return tableName.substring(prefix.length());
        }
        
        return tableName;
    }
    
    /**
     * 驼峰转下划线
     */
    public static String toUnderlineCase(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (Character.isUpperCase(c)) {
                if (i > 0) {
                    result.append("_");
                }
                result.append(Character.toLowerCase(c));
            } else {
                result.append(c);
            }
        }
        
        return result.toString();
    }
    
    /**
     * 驼峰转连字符
     */
    public static String toHyphenCase(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (Character.isUpperCase(c)) {
                if (i > 0) {
                    result.append("-");
                }
                result.append(Character.toLowerCase(c));
            } else {
                result.append(c);
            }
        }
        
        return result.toString();
    }
    
    /**
     * 生成名称模型
     */
    public static NameModel getNameModel(String tableName, String prefix) {
        // 移除前缀
        String nameWithoutPrefix = removePrefix(tableName, prefix);
        
        // 生成不同格式的名称
        String upperCamel = toPascalCase(nameWithoutPrefix);
        String lowerCamel = toCamelCase(nameWithoutPrefix);
        String underscore = nameWithoutPrefix.toLowerCase();
        String hyphen = toHyphenCase(upperCamel);
        String lowercase = upperCamel.toLowerCase();
        String uppercase = upperCamel.toUpperCase();
        
        return new NameModel(upperCamel, lowerCamel, underscore, hyphen, lowercase, uppercase);
    }
} 